package com.asteros.crypto.utils.content;

import com.asteros.crypto.ServiceFactory;
import com.asteros.crypto.config.AppConfig;
import com.asteros.crypto.utils.services.CryptoService;
import com.asteros.crypto.utils.services.CryptoServiceConfig;

public class WebServerCM implements ContentManager {
	private WebServerCMUtils cmUtils = new WebServerCMUtils_Stub();
//	private WebServerCMUtils cmUtils = new WebServerCMUtils_Prod();

	private CryptoService cryptoService = null;
	private CryptoServiceConfig cryptoServiceConfig = null;

	private CryptoServiceConfig getCryptoServiceConfig()
	{
		if (null == cryptoServiceConfig ) {
			AppConfig.getInstance().defaultServerInit();

			//		if (this.keyStorePKCS12Button.isSelected())
			//		{
			cryptoServiceConfig = AppConfig.getInstance().getSCPKCS12CryptoServiceConfig();
			//		}
			//		else if (this.keyStoreRutokenButton.isSelected())
			//		{
			//			cconfig = AppConfig.getInstance().getRutokenCryptoServiceConfig();
			//		}
			//		else
			//		{
			//			cconfig = AppConfig.getInstance().getEtokenCryptoServiceConfig();
			//		}
		}
		return cryptoServiceConfig;
	}
	
	private CryptoService getCryptoService() throws Exception {
		if (null == cryptoService ) {
			cryptoService = ServiceFactory.getInstance().createCryptoService(getCryptoServiceConfig());
		}
		return cryptoService;
	}

	public byte[] getContent(String userId, String documentId) throws Exception
	{
		System.out.println("WebServerCM.getContent>>");

		byte[] buf = cmUtils.getContent(userId, documentId);
		System.out.println(buf.toString());
		System.out.println(buf.length);

		System.out.println("WebServerCM.getContent<<");
		return buf;
	}

	public byte[] getSignedData(String userId, String documentId) throws Exception {
		System.out.println("WebServerCM.getSignedData>>");

		byte[] buf = cmUtils.getSignedData(userId, documentId);

		System.out.println("WebServerCM.getSignedData<<");
		return buf;
	}

	public void saveSignedData(String userId, String documentId, byte[] signedData)	throws Exception {
		System.out.println("WebServerCM.saveSignedData>>");

		byte[] signedDataNew = null;
		byte[] signedDataOld = this.getSignedData(userId, documentId);

		if (null == signedDataOld) {
			System.out.println("������� ���������� �������>>");
			signedDataNew = signedData;
		} else {
			System.out.println("������� ��������>>");
			signedDataNew = getCryptoService().merge(signedDataOld, signedData);
		}
		
		System.out.println("�������� ����� ����� �������>>");
		if (!this.verify(signedDataNew, this.getContent(userId, documentId))) {
			throw new Exception("�� ������� ��������� �������");
		}
		
		cmUtils.saveSignedData(userId, documentId, signedDataNew);

		System.out.println("WebServerCM.saveSignedData<<");
	}

	public boolean verifyDocumentSignature(String userId, String documentId) throws Exception {
		System.out.println("WebServerCM.saveSignedData>>");		
		byte[] content = this.getContent(userId, documentId);
		byte[] signedData = this.getSignedData(userId, documentId);
		
		boolean res = false ;
		if (signedData != null) {
			res = this.verify(signedData, content); 
		}
		
		System.out.println("WebServerCM.saveSignedData<<");
		return res;
	}
	
	private boolean verify(byte[] signedData, byte[] content) throws Exception{
		return getCryptoService().verify(signedData, content);
	}
}
